package com.justgame.project.service.system.service;

import com.justgame.project.cloud.common.entity.PageRequest;
import com.justgame.project.cloud.common.exception.Exc;
import com.justgame.project.cloud.common.util.ObjUtil;
import com.justgame.project.cloud.common.util.RedisUtil;
import com.justgame.project.service.api.dto.SysHomeColumnDTO;
import com.justgame.project.service.api.vo.SysHomeColumnVO;
import com.justgame.project.service.system.mapper.SysHomeColumnMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
@Slf4j
@Transactional
public class SysHomeColumnService {

    @Resource
    private SysHomeColumnMapper sysHomeColumnMapper;

    @Resource
    private RedisUtil redisUtil;

    @Cacheable(cacheNames = {"SysHomeColumns"})
    public PageRequest<SysHomeColumnVO> selectVoList(SysHomeColumnDTO dto, PageRequest<SysHomeColumnVO> pageRequest) {
        pageRequest.startPage();
        List<SysHomeColumnVO> voList = sysHomeColumnMapper.Select(dto);
        return pageRequest.BuildPageInfo(voList);
    }

    @CacheEvict(cacheNames = {"SysHomeColumns"})
    public boolean delete(SysHomeColumnDTO dto) {
        ObjUtil.inject(dto, false);
        return sysHomeColumnMapper.delete(dto) > 0;
    }

    @CacheEvict(cacheNames = {"SysHomeColumns"})
    public boolean updateById(SysHomeColumnDTO dto) {
        if (StringUtils.isBlank(dto.getId())) {
            throw new Exc("id不能为空！");
        }
        ObjUtil.inject(dto, false);
        return sysHomeColumnMapper.updateById(dto) > 0;
    }

    @CacheEvict(cacheNames = {"SysHomeColumns"})
    public boolean save(SysHomeColumnDTO dto) {
        ObjUtil.inject(dto, true);
        return sysHomeColumnMapper.save(dto) > 0;
    }

    @Cacheable(cacheNames = {"SysHomeColumns"})
    public List<SysHomeColumnVO> selectIndexList() {
        return sysHomeColumnMapper.Select(new SysHomeColumnDTO());
    }
}
